using System;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Universe.OneC
{
    public static class ExtensionMethods
    {
        public static object Call(this object target, string name, params object[] args)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            BindingFlags invokeAttr = BindingFlags.InvokeMethod | BindingFlags.Public ;
            return target.GetType().InvokeMember(name, invokeAttr, null, target, args);
        }

        public static void ReleaseComObject(this object target)
        {
            if (target != null && Marshal.IsComObject(target))
                Marshal.ReleaseComObject(target);
        }

        public static object Get(this object target, string name)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            BindingFlags invokeAttr = BindingFlags.GetProperty | BindingFlags.Public;
            return target.GetType().InvokeMember(name, invokeAttr, null, target, null);
        }

        public static void Set(this object target, string name, object value)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            BindingFlags invokeAttr = BindingFlags.SetProperty | BindingFlags.Public;
            target.GetType().InvokeMember(name, invokeAttr, null, target, new object[] { value });
        }
    }
}